package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CustomerMapper;
import com.qf.ssm.po.Customer;
import com.qf.ssm.po.EmergencyContact;
import com.qf.ssm.po.Invoice;
import com.qf.ssm.qo.CustomerQO;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CustomerService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.CustomerSelectVO;
import com.qf.ssm.vo.CustomerUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.sql.SQLIntegrityConstraintViolationException;


/**
 * @author chengqiang
 * @Create 2024-01-02-15:50
 * @Description:
 */
@Service
@Repository// 添加事务
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional
    // 添加客户
    public ResponseResult<String> insertCustomer(CustomerQO customerQO) {
        // QO转PO
        Customer customer = CommonBeanutils.convertTo(customerQO, Customer::new);
        Invoice invoice = CommonBeanutils.convertTo(customerQO.getInvoice(), Invoice::new);
        EmergencyContact emergencyContact = CommonBeanutils.convertTo(customerQO.getEmergencyContact(), EmergencyContact::new);

        // 添加发票
            customerMapper.insertInvoice(invoice);
        // 添加紧急联系人
        customerMapper.insertEmergencyContact(emergencyContact);
        // 主键回显，将主键加入客户PO
        customer.setEmergencyContactId(emergencyContact.getEid());
        customer.setInvoiceId(invoice.getInvoiceId());

        try {
            customerMapper.insertCustomer(customer);
            return ResponseResult.success("添加客户信息成功");
        } catch (Exception e) {
            Throwable cause = e.getCause();
            if (cause instanceof SQLIntegrityConstraintViolationException) {
                return ResponseResult.error(ResultStatus.SQL_INSERT_CUSTOMER);
            }
            throw new RuntimeException();
        }


    }

    @Override
    @Transactional
    // 根据条件查询客户列表
    public PageInfo<CustomerSelectVO> queryByCustomer(int pageNum,int pageSize,CustomerQO customerQO){


        Customer customer = CommonBeanutils.convertTo(customerQO, Customer::new);


        return  CommonBeanutils.convertPageInfo(
                PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                        () -> customerMapper.selectByCustomer(CommonBeanutils.convertTo(customerQO,Customer::new),customerQO.getCharacters())),
                new PageInfo<>(), CustomerSelectVO::new);
    }
    @Override
    @Transactional
    public ResponseResult<String> modifyCustomer(CustomerQO customerQO){

        Customer customer = CommonBeanutils.convertTo(customerQO, Customer::new);
        // 如果需要修改发票信息或联系人信息

            CustomerUpdateVO customerUpdateVO=null;
            // 如果客户id不为空
            if(customerQO.getId()!=null){
                // 发票id和紧急联系人id
                customerUpdateVO = customerMapper.selectByIdCustomer(customerQO.getId());
                if(customerUpdateVO==null){
                    throw new ServiceException(ResultStatus.USER_NO_EXIST);
                }
            }else {
                throw new ServiceException(ResultStatus.USER_NO_EXIST);
            }

            Invoice invoice = CommonBeanutils.convertTo(customerQO.getInvoice(), Invoice::new);
            EmergencyContact emergencyContact = CommonBeanutils.convertTo(customerQO.getEmergencyContact(), EmergencyContact::new);

            // 修改紧急联系人
            if(!ObjectUtils.isEmpty(customerQO.getEmergencyContact())){
                emergencyContact.setEid(customerUpdateVO.getEid());
                customerMapper.updateEmergencyContact(emergencyContact);
            }

            // 修改发票
            if(!ObjectUtils.isEmpty(customerQO.getInvoice())){
                invoice.setInvoiceId(customerUpdateVO.getIid());
                customerMapper.updateInvoice(invoice);
            }


        try {
            customerMapper.updateCustomer(customer);
            return ResponseResult.success("修改客户信息成功");
        } catch (Exception e) {
            return ResponseResult.error(ResultStatus.SQL_INSERT_CUSTOMER);
        }
        // 修改客户信息

    }




}
